package com.example.children.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.children.DTO.BehaviorTeacherDTO;
import com.example.children.DTO.HealthCheckTeacherDTO;
import com.example.children.Model.Attendance;
import com.example.children.Model.Behavior;
import com.example.children.Service.HealthCheckService;
import com.example.children.Util.Tools;
import com.example.children.Util.UUIDGenerator;
import com.example.children.Util.tool;
import com.example.children.Model.HealthCheck;
import com.example.children.Model.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/HealthChecks")
public class HealthCheckController {

    @Autowired
    private HealthCheckService healthCheckService;

    // 分页所有体检记录
    @GetMapping("/All")
    public ResponseModel<Page<HealthCheckTeacherDTO>> getAttendanceByStudent(
            @RequestParam(defaultValue = "1") int pageNo,  // 默认页码为1
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        System.out.println("进入查询体检All方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
        Page<HealthCheckTeacherDTO> healthCheckTeacherDTO=healthCheckService.GetAllHealthCheck(pageNo,pageSize);
        if(healthCheckTeacherDTO == null || healthCheckTeacherDTO.getRecords().isEmpty()){
            System.out.println("没有体检数据");
            msg= tool.fail;
        }
        return new ResponseModel<>(healthCheckTeacherDTO,code,msg,null);//响应给前端的对象
    }

    @GetMapping("/getHealthChecks")
    public ResponseModel<List<HealthCheck>> getHealthChecks(@RequestParam String studentId) {
        System.out.println("进入getHealthChecks方法："+studentId);
        Integer code= Tools.Success;
        String msg= tool.success;
        List<HealthCheck> healthChecks=healthCheckService.getHealthChecksByStudent(studentId);
        if(healthChecks==null||healthChecks== new ArrayList<HealthCheck>()||healthChecks.isEmpty()){
            msg= tool.fail;
        }
        return new ResponseModel<>(healthChecks,code,msg,null);//根据学生ID查询体检记录
    }

    @PostMapping("/addHealthCheck")  //添加
    public ResponseModel<HealthCheck> addHealthCheck(@RequestBody HealthCheck healthCheck) {
        System.out.println("进入addHealthCheck方法：");
        String uuid = UUIDGenerator.generateNumericUUID(8);
        healthCheck.setCheckId(uuid);
        Integer code= Tools.Success;
        String msg= tool.success;
        if(!healthCheckService.save(healthCheck)){
            msg= tool.fail;
        }
        return new ResponseModel<>(healthCheck,code,msg,null);//保存体检记录
    }

    // 更新体检记录
    @PostMapping("/updateHealthCheck")
    public ResponseModel<HealthCheck> updateHealthCheck(@RequestBody HealthCheck healthCheck) {
        System.out.println("进入updateHealthCheck方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
        if(!healthCheckService.updateById(healthCheck)){
            msg= tool.fail;
        }
        return new ResponseModel<>(healthCheck,code,msg,null);//响应给前端的对象
    }

    // 删除体检记录
    @DeleteMapping("/deleteHealthCheck")
    public ResponseModel<String> deleteHealthCheck(@RequestParam String checkId) {
        System.out.println("进入deleteHealthCheck方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
        if(!healthCheckService.removeById(checkId)){
            msg= tool.fail;
        }
        return new ResponseModel<>(checkId,code,msg,null);// 根据ID删除行为记录
    }


    // 日期搜索出勤记录
    @PostMapping("/selectByDateHealthCheck")
    public ResponseModel<List<HealthCheck>> selectByDateHealthCheck(@RequestBody HealthCheck healthCheck) {
        System.out.println("进入selectByDateHealthCheck方法："+healthCheck.getCheckDate());
        Integer code= Tools.Success;
        String msg= tool.success;
        List<HealthCheck> healthChecks=healthCheckService.selectByDateHealthCheck(healthCheck);
        if(healthChecks == null || healthChecks.isEmpty())
            msg=tool.fail;
        return new ResponseModel<>(healthChecks, code, msg, null); // 成功响应
    }


    // 搜索记录
    @PostMapping("/AdminSelectByDateHealthCheck")
    public ResponseModel<List<HealthCheckTeacherDTO>> AdminSelectByDateHealthCheck(@RequestBody HealthCheckTeacherDTO healthCheckTeacherDTO) {
        System.out.println("进入AdminSelectByDateHealthCheck方法："+healthCheckTeacherDTO.getCheckDate());
        Integer code= Tools.Success;
        String msg= tool.success;
        List<HealthCheckTeacherDTO> attendances=healthCheckService.AdminselectByDateSend(healthCheckTeacherDTO);
        if(attendances == null || attendances.isEmpty())
            msg=tool.fail;
        return new ResponseModel<>(attendances, code, msg, null); // 成功响应
    }

}

